<template>
  <div>
    <el-main>
      <el-header>
        <el-form
          ref="searchForm"
          size="small"
          :model="searchModel"
          :inline="true"
          @keyup.enter.native="search"
        >
          <el-row :gutter="20">
                <el-col :span="4">
              <el-button icon="el-icon-refresh" size="small" @click="resetSearch"
              >重置
              </el-button>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="small"
                @click="search"
              >搜索
              </el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-header>
      <el-table
        ref="table"
        :data="tableData.records"
        :border="true"
        style="width: 100%"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-link v-if="handleExist('roleMenuUpdate')" type="primary" :underline="false" icon="el-icon-edit" @click="handleUpdate(row)">编辑</el-link>
            <el-link v-if="handleExist('roleMenuDelete')" type="primary" :underline="false" icon="el-icon-delete" @click="handleDelete(row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <pagination
          v-show="tableData.total > 0"
          :total="tableData.total"
          :page.sync="tableData.current"
          :limit.sync="tableData.size"
          @pagination="search"
        />
      </div>
      <el-footer>
        <el-button v-if="handleExist('roleMenuInsert')"
                   type="primary"
                   icon="el-icon-plus"
                   size="small"
                   @click="handleInsert"
        >新增
        </el-button
        >
        <el-button v-if="handleExist('roleMenuEnables')"
                   type="primary"
                   icon="el-icon-unlock"
                   size="small"
                   @click="handleEnables"
        >启用
        </el-button
        >
        <el-button v-if="handleExist('roleMenuDisables')"
                   type="primary"
                   icon="el-icon-lock"
                   size="small"
                   @click="handleDisables"
        >禁用
        </el-button
        >
        <el-button v-if="handleExist('roleMenuDeletes')"
                   type="primary"
                   icon="el-icon-delete"
                   size="small"
                   @click="handleDeletes"
        >删除
        </el-button
        >
      </el-footer>
      <RoleMenuShow :show.sync="roleMenuVisible" :role-menu="roleMenu" />
    </el-main>
  </div>
</template>

<script>
  import Pagination from "@/components/Pagination";
  import RoleMenuShow from "@/views/content/role-menu/RoleMenuShow.vue";
  import {deleteRoleMenu,deletesRoleMenu,enablesRoleMenu,disablesRoleMenu,pageRoleMenu} from "@/api/system/roleMenu";
  import {Status} from "@/utils/constant/status";
  import {parseTime} from "@/utils/common";
  import {ApiError} from "@/utils/errors";
  import {existFunction} from "@/utils/auth";

  export default {
    components: {Pagination}
    data() {
      return {
        Status,
        selection:[],
        searchModel:{
        },
        tableData: {
          records: [],
          total: 0,
          current: 1,
          size: 10
        }
      }
    },
    created() {
      this.search();
    },
    methods: {
      handleExist(functionCode){
        return existFunction(functionCode);
      },
      handleSelectionChange(selection){
        this.selection = selection;
      },
      handleInsert() {
        this.$router.push({path: "role-menu-insert"});
      },
      handleUpdate(row) {
        this.$router.push({path: "role-menu-update",query:{id:row.id}});
      },
      handleDelete(row) {
        this.$confirm(`是否删除角色菜单表 ${row.name} ?`, "提示", {
          type: "warning"
        })
          .then(() => {
            const params = {
              id: row.id
            };
            deleteRoleMenu(params).then(res => {
              if (res.status == 200) {
                if (!res.data.success) {
                  return this.$message.error(ApiError.msg(res.data.message));
                }
                this.$message.success("删除角色菜单表成功");
                this.search();
              }
            });
          })
          .catch(() => {});
      },
      handleDeletes() {
        this.$confirm(`是否删除选中的${this.selection.length}个角色菜单表?`, "提示", {
          type: "warning"
        })
          .then(() => {
            const params = {
              ids: this.selection
            };
            deletesRoleMenu(params).then(res => {
              if (res.status == 200) {
                if (!res.data.success) {
                  return this.$message.error(ApiError.msg(res.data.message));
                }
                this.$message.success(`成功删除${res.data.data.count}个角色菜单表`);
                this.search();
              }
            });
          })
          .catch(() => {});
      },
      handleEnables() {
        const params = {
          ids: this.selection
        };
        enablesRoleMenu(params).then(res => {
          if (res.status == 200) {
            if (!res.data.success) {
              return this.$message.error(ApiError.msg(res.data.message));
            }
            this.$message.success(`成功启用${res.data.data.count}个角色菜单表`);
            this.search();
          }
        });
      },
      handleDisables() {
        const params = {
          ids: this.selection
        };
        disablesRoleMenu(params).then(res => {
          if (res.status == 200) {
            if (!res.data.success) {
              return this.$message.error(ApiError.msg(res.data.message));
            }
            this.$message.success(`成功禁用${res.data.data.count}个角色菜单表`);
            this.search();
          }
        });
      },
      search() {
        const params = {
          status: this.searchModel.status,
          current: this.tableData.current,
          size: this.tableData.size
        };
        pageRoleMenu(params).then(res => {
          if (res.status == 200) {
            if (!res.data.success) {
              return this.$message.error(ApiError.msg(res.data.message));
            }
            const data = res.data.data;
            this.tableData.records = data.records;
            this.tableData.total = data.total;
            this.tableData.current = data.current;
            this.tableData.size = data.size;
          }
        });
      },
      resetSearch(){
        this.searchModel.status = null;
        this.searchModel.createTime = null;
      },
      statusFormatter(row) {
        return Status[row.status]["name"];
      },
      datetimeFormatter(row) {
        return parseTime(row["createTime"]);
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "@/styles/full-form";
  @import "@/styles/common-table";
</style>
